/**
 * Licensed under the Apache License, Version 2.0 (the "License"); you may
 * not use this file except in compliance with the License. You may obtain
 * a copy of the License at
 * 
 *         http://www.apache.org/licenses/LICENSE-2.0
 *         
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License. Unless required by applicable law or
 * agreed to in writing, software distributed under the License is
 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the specific
 * language governing permissions and limitations under the License.
 */
package org.rhok.pdx.ncn.application;

import org.rhok.pdx.ncn.R;
import org.rhok.pdx.ncn.listeners.MapPlotter;
import org.rhok.pdx.ncn.listeners.TrackingLocationListener;
import org.rhok.pdx.ncn.listeners.TrackingPhoneStateListener;
import org.rhok.pdx.ncn.listeners.WifiSignalTracker;

import android.content.Context;
import android.content.Intent;
import android.location.LocationListener;
import android.location.LocationManager;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;

import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;

/**
 * Generates the default display of a pointer on a map with signal strenght markers.
 * 
 * @author afitzgibbon
 */
public class MapMenu extends MapActivity {
    
    private ApplicationContext context;
    private LocationListener locListener;
    private PhoneStateListener psListener;
    private LocationManager locMgr;
    private TelephonyManager teleMgr;
    private WifiManager wifiMgr;
    private WifiSignalTracker wifiTracker;
    private MapView mapView;
    private MapPlotter mapPlotter;
    private MyLocationOverlay locOverlay;
    private MapController mapController;

    /**
     * Run on creation of the application. Initializes the context, sets the layout, and begins
     * tracking data. This method should never be called directly.
     * @param savedInstanceState Information from saved instance of application, passed in by system
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.map_layout);

        context = new ApplicationContext(this);
        
        initDataTracking();

        mapView = (MapView) findViewById(R.id.mapview);
        mapView.setBuiltInZoomControls(true);
        context.mapView(mapView);
        mapController = mapView.getController();

        locOverlay = new MyLocationOverlay(this, mapView);
        mapView.getOverlays().add(locOverlay);
        locOverlay.enableCompass();
        locOverlay.enableMyLocation();
        locOverlay.runOnFirstFix(new Runnable() {
            public void run() {
                mapController.animateTo(locOverlay.getMyLocation());
            }
        });

        mapPlotter = new MapPlotter(context);
        mapPlotter.start();
    }
    
    /**
     * Initializes data collection procedures. Starts collections location, cell signal, and wifi
     * signal information. Each listener / data collector stores said data in the application
     * SQLite database.
     */
    private void initDataTracking() {
        locListener = new TrackingLocationListener(context);
        psListener = new TrackingPhoneStateListener(context);

        locMgr = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        locMgr.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 10,
                locListener);

        teleMgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        teleMgr.listen(psListener, PhoneStateListener.LISTEN_SIGNAL_STRENGTH);

        wifiMgr = (WifiManager) getSystemService(Context.WIFI_SERVICE);
        wifiTracker = new WifiSignalTracker(context, wifiMgr);
        wifiTracker.start();
    }

    /* (non-Javadoc)
     * @see com.google.android.maps.MapActivity#onResume()
     */
    @Override
    public void onResume() {
        super.onResume();

        locOverlay.enableCompass();
    }

    /* (non-Javadoc)
     * @see com.google.android.maps.MapActivity#onPause()
     */
    @Override
    public void onPause() {
        super.onPause();

        locOverlay.disableCompass();
    }

    /* (non-Javadoc)
     * @see com.google.android.maps.MapActivity#isRouteDisplayed()
     */
    @Override
    protected boolean isRouteDisplayed() {
        return false;
    }

    /* (non-Javadoc)
     * @see android.app.Activity#onCreateOptionsMenu(android.view.Menu)
     */
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();

        inflater.inflate(R.menu.data_menu, menu);

        return true;
    }

    /* (non-Javadoc)
     * @see android.app.Activity#onOptionsItemSelected(android.view.MenuItem)
     */
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        switch (item.getItemId()) {
        case R.id.raw_data_menu:
            showData();
            return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    }

    /**
     * Switch to the raw data view.
     */
    private void showData() {
        Intent intent = new Intent(this, DataMenu.class);
        startActivity(intent);
        finish();
    }
}
